package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.AttachmentEntity;
import com.antler.smt.entity.MemberEntity;
import com.antler.smt.entity.SupplierEntity;
import com.antler.smt.portal.mapper.*;
import com.antler.smt.request.portal.SearchReq;
import com.antler.smt.response.admin.GetOrderListResp;
import com.antler.smt.response.admin.GetUserListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.SearchOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangyi
 * @date 2018/9/19 17:45
 */
@Service
public class SearchOrderServiceImpl implements SearchOrderService {

    @Autowired
    private SearchInfoMapper searchInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private AttachmentEntityMapper attachmentEntityMapper;

    @Autowired
    private SupplierEntityMapper supplierEntityMapper;

    @Autowired
    private MemberEntityMapper memberEntityMapper;

    /**
     * 条件查询订单结果
     *
     * @param searchReq
     * @return
     */
    @Override
    public Result searchOrder(SearchReq searchReq) {
        List<GetOrderListResp> orderListResps = searchInfoMapper.selectOrderInfo(searchReq);
        orderListResps.stream()
                .forEach(e -> {
                    Long connectUserId = e.getConnectUserId();
                    Long supplierId = e.getSupplierId();
                    GetUserListResp userInfo = userInfoMapper.getUserInfo(connectUserId);
                    //商务专员id
                    Long businessAffairsUserId = e.getBusinessAffairsUserId();

                    if(null != userInfo) {
                        //插入真实姓名
                        e.setRealName(userInfo.getRealName());
                    }
                    GetUserListResp businessUserInfo = userInfoMapper.getUserInfo(businessAffairsUserId);
                    if(null  != businessUserInfo) {
                        e.setBusinessUserName(businessUserInfo.getRealName());
                    }
                    //报告文件名id
                    Long reportId = e.getTestReportAttachmentId();
                    AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(reportId);
                    //添加报告名
                    if(null != attachmentEntity){
                        e.setReportName(attachmentEntity.getFileName());
                    }
                    if(e.getStatus() != 100) {
                        SupplierEntity supplierEntity = supplierEntityMapper.selectByPrimaryKey(supplierId);
                        if(null != supplierEntity) {
                            MemberEntity memberEntity = memberEntityMapper.selectByPrimaryKey(supplierEntity.getMemberId());
                            e.setNickName(memberEntity.getNickName());
                        }else {
                            e.setNickName(null);
                        }
                    }
                });

        return new Result(orderListResps);
    }
}
